<?php

/**
 * reserva actions.
 *
 * @package    gestdent
 * @subpackage reserva
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class reservaActions extends sfActions
{

  public function executeIndex(sfWebRequest $request)
  {
      //FILTRO
      $this->filtro = new GestdentReservaFormFilter();
      
      //LISTADO DE HORAS
      $this->gestdent_hora_list = GestdentHoraPeer::doSelect(new Criteria());

      //QUERY PARA LISTADO DE BOX
      $MyCriterioBox = new Criteria();
      $MyCriterioBox->add(GestdentBoxPeer::SUCURSAL,1);
      $this->gestdent_box_list = GestdentBoxPeer::doSelect($MyCriterioBox);


      //QUERY DE RESERVAS
      $MyCriterioReservas = new Criteria();
      $MyCriterioReservas->add(GestdentReservaPeer::FECHA,"2010-01-05");
      //$MyCriterioReservas->add(GestdentReservaPeer::SUCURSAL,1);
      $this->gestdent_reserva_list = GestdentReservaPeer::doSelect($MyCriterioReservas);


  }

  public function executeShow(sfWebRequest $request)
  {
    $this->gestdent_reserva = GestdentReservaPeer::retrieveByPk($request->getParameter('id'));
    $this->forward404Unless($this->gestdent_reserva);
  }

  public function executeVer(sfWebRequest $request)
  {
    $this->gestdent_reserva = GestdentReservaPeer::retrieveByPk($request->getParameter('id'));
    $this->forward404Unless($this->gestdent_reserva);
  }



  public function executeNew(sfWebRequest $request)
  {
    $this->form = new GestdentReservaForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post'));

    $this->form = new GestdentReservaForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($gestdent_reserva = GestdentReservaPeer::retrieveByPk($request->getParameter('id')), sprintf('Object gestdent_reserva does not exist (%s).', $request->getParameter('id')));
    $this->form = new GestdentReservaForm($gestdent_reserva);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    $this->forward404Unless($gestdent_reserva = GestdentReservaPeer::retrieveByPk($request->getParameter('id')), sprintf('Object gestdent_reserva does not exist (%s).', $request->getParameter('id')));
    $this->form = new GestdentReservaForm($gestdent_reserva);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($gestdent_reserva = GestdentReservaPeer::retrieveByPk($request->getParameter('id')), sprintf('Object gestdent_reserva does not exist (%s).', $request->getParameter('id')));
    $gestdent_reserva->delete();

    $this->redirect('reserva/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $gestdent_reserva = $form->save();

      $this->redirect('reserva/edit?id='.$gestdent_reserva->getId());
    }
  }

  public function executeFiltrar2(sfWebRequest $request)
  {
    //$this->filtro = new GestdentPacienteFormFilter();
    // COMENTARIO: Construye un objeto criteria con el valor de los filtros
    //$criteria = $this->filtro->buildCriteria($request->getParameter('gestdent_paciente_filters'));
    //$this->gestdent_paciente_list = GestdentPacientePeer::doSelect($criteria);
    //$this->setTemplate('search');

    //$this->filtro = new GestdentReservaFormFilter();
    //$criteria = $this->filtro->buildCriteria($request->getPostParameter('gestdent_reserva_filters'));
    //$this->gestdent_reserva_list = GestdentReservaPeer::doSelect($criteria);
    //$this->setTemplate('filtra');
  }

  public function executeFiltrar(sfWebRequest $request)
  {
    $this->filtro = new GestdentReservaFormFilter();
    $criteria = $this->filtro->buildCriteria($request->getParameter('gestdent_reserva_filters'));
    $this->gestdent_reserva_list = GestdentReservaPeer::doSelect($criteria);
    $this->setTemplate('filtrar');
  }



  public function executeListarxdia(sfWebRequest $request)
  {

    $this->getUser()->setSessionDate($request->getParameter('mydate'));

    //OBTIENE PARAMETROS DESDE FORMULARIO (WIDGET'S)
    //$param_sucursal = $request->getParameter('sucursal_id');
    $param_dia = $this->getUser()->getSessionDia();//$request->getParameter('dia_id');

    //OBTINE LISTADO DE HORAS
    $this->gestdent_hora_list = GestdentHoraPeer::doSelect(new Criteria());

    //OBTINE LISTADO DE BOX'S DE LA SUCURSAL
    $this->gestdent_box_list = GestdentBoxPeer::filtrarxsucursal($request->getParameter('sucursal_id'));

    //OBTIENE LISTADO DE HORARIOS
    $this->gestdent_horario3_list = GestdentHorario3Peer::filtrarxdia($request->getParameter('sucursal_id'), $param_dia);

    //OBTIENE LISTADO DE RESERVAS
    $MyCriterioReservas = new Criteria();
    $MyCriterioReservas->add(GestdentReservaPeer::FECHA,$this->getUser()->getSessionDate());
    //$MyCriterioReservas->add(GestdentReservaPeer::SUCURSAL,1);
    $this->gestdent_reserva_list = GestdentReservaPeer::doSelect($MyCriterioReservas);

    //SETEA FORMULARIO
    $this->formulario = new GestdentSucursalFormCustom();

    //SETEA TEMPLATE
    $this->setTemplate('index');

    }

  public function executeSave(sfWebRequest $request)
  {

    //CREAMOS LA RESERVA
    $mireserva = new GestdentReserva();
    $mireserva->setPersonal($request->getParameter('Doctor_id'));
    $mireserva->setHora($request->getParameter('hora_id'));

    $mireserva->setFecha($this->getUser()->getSessionDate());
    //$mireserva->setSucursal(1);
    $mireserva->setPaciente($this->getUser()->getSessionPacienteId());
    //$mireserva->setPaciente(1);
    

    $mireserva->setConfirmado(false);
    $mireserva->setRecepcionado(false);
    $mireserva->setTipoasistencia(1);
    $mireserva->setObservacion();

    //SETEA TEMPLATE
    $this->setTemplate('save');


    //GUARDAMOS LA RESERVA... ESTE DEBE LLAMARSE DESDE EL BOTON.
    if($mireserva->save())
    {
        return sfView::SUCCESS;
    }
    else
    {
        return sfView::ERROR;
    }

  }

  public function executeSave2(sfWebRequest $request)
  {
    //CREAMOS LA RESERVA
    $mireserva = new GestdentReserva();

    //SE ASIGNA DOCTOR A LA RESERVA
    $mireserva->setPersonal($request->getParameter('Doctor_id'));
    
    //SE ASIGNA HORA A LA RESERVA
    $mireserva->setHora($request->getParameter('hora_id'));

    //SE ASIGNA FECHA A LA RESERVA
    $mireserva->setFecha($this->getUser()->getSessionDate());

    //SE ASIGNA PACIENTE A LA RESERVA
    $mireserva->setPaciente($this->getUser()->getSessionPacienteId());

    //SE GUARDA LA RESERVA EN LA VARIABLE DE SESSION
    $this->getUser()->setSessionReserva($mireserva);

    //SETEA FORMULARIO PARA AGREGAR EL RESTO DE LOS DATOS DE LA RESERVA
    $this->formulario = new GestdentReservaFormCustom1();

    //SE SETEA TEMPLATE PARA AGREGAR EL RESTO DE LOS DATOS DE LA RESERVA
    $this->setTemplate('editar');

  }

  public function executeSave3(sfWebRequest $request)
  {
    //OBTENEMOS LA RESERVA LA RESERVA
    $mireserva = $this->getUser()->getSessionReserva();

    $mireserva->setConfirmado(false);
    $mireserva->setRecepcionado(false);
    
    $mireserva->setTipoasistencia($request->getParameter('tipoasistencia_id'));
    //$mireserva->setTipoasistencia(1);

    $mireserva->setObservacion($request->getParameter('observacion'));

    //SETEA TEMPLATE
    $this->setTemplate('save');

    //GUARDAMOS LA RESERVA... ESTE DEBE LLAMARSE DESDE EL BOTON.
    if($mireserva->save())
    {
        return sfView::SUCCESS;
    }
    else
    {
        return sfView::ERROR;
    }
  }
  
  public function executeConfirmar(sfWebRequest $request)
  {
    $this->forward404Unless($gestdent_reserva = GestdentReservaPeer::retrieveByPk($request->getParameter('id')), sprintf('Object gestdent_reserva does not exist (%s).', $request->getParameter('id')));

    $gestdent_reserva->setConfirmado(true);



  }


  public function executeListarxdoctor(sfWebRequest $request)
  {

      $var = $request;

    //OBTIENE LISTADO DE RESERVAS POR DOCTOR
    $MyCriterioReservas = new Criteria();
    $MyCriterioReservas->add(GestdentReservaPeer::PERSONAL,$request->getParameter('personal_id'));
    $MyCriterioReservas->add(GestdentReservaPeer::FECHA,$request->getParameter('fecha'));
    $this->gestdent_reserva_list = GestdentReservaPeer::doSelect($MyCriterioReservas);
  }
  

}